#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

const int N = 100004;
int nxt[N], pre[N], idx[N];
int used;
int new_node(int id, int p = -1, int n = -1) {
  idx[used] = id;
  pre[used] = p;
  nxt[used] = n;
  return used ++;
}
struct List {
  int head, tail, sz, cap;
  List(int n) {
    head = new_node(-1);
    tail = new_node(-1);
    nxt[head] = tail;
    pre[tail] = head;
    sz = 0;
    cap = n;
  }
  void insert(int node) {
    // head   node   next
    int next = nxt[head];
    nxt[node] = next;
    pre[node] = head;
    nxt[head] = node;
    pre[next] = node;
    sz ++;
  }
  void erase(int node) {
    int prev = pre[node], next = nxt[node];
    nxt[prev] = next;
    pre[next] = prev;
    sz --;
  }
  int check_erase() {
    if (sz > cap) {
      int node = pre[tail];
      erase(node);
      return node;
    }
    return -1;
  }
  void output() {
    for (int node = nxt[head]; node != tail; node = nxt[node]) {
      cout << idx[node] << ' ';
    }
    cout << endl;
  }
};

void solve() {
  int n1, n2;
  cin >> n1 >> n2;
  int Q;
  cin >> Q;
  memset(nxt, -1, sizeof(nxt));
  memset(pre, -1, sizeof(pre));
  List q1(n1), q2(n2);
  vector<pair<int, int>> nodes(10001, {-1, -1});
  // [index] -> [qid, nodeid]
  while (Q --) {
    int x;
    cin >> x;
    if (nodes[x].first == -1) {
      // 不在队列, step 1
      int node = new_node(x);
      q2.insert(node);
      nodes[x] = {2, node};
      // 检查, step 3
      int rm_node = q2.check_erase();
      if (rm_node != -1) nodes[idx[rm_node]] = {-1, -1};
    } else {
      // 在队列中， step 2
      auto [qid, node] = nodes[x];
      if (qid == 2) q2.erase(node);
      else q1.erase(node);
      q1.insert(node);
      nodes[x] = {1, node};
      // 检查，step 3
      int tail_node = q1.check_erase();
      if (tail_node == -1) continue;
      q2.insert(tail_node);
      nodes[idx[tail_node]] = {2, tail_node};
      // 检查，step 3
      int rm_node = q2.check_erase();
      if (rm_node != -1) nodes[idx[rm_node]] = {-1, -1};
    }
  }
  q1.output();
  q2.output();
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int T = 1;
  // cin >> T;
  for (int cas = 1; cas <= T; cas ++) {
    solve();
  }
}